<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
* Initialize messages that will be shown to the user.
* @access private 
*/
function init_messages($content)
{
    $ci = &get_instance();
    
    $ci->messages['add_success'] = $content . ' Successfully Created';
    $ci->messages['edit_success'] = $content . ' Successfully Edited';
    $ci->messages['delete_success'] = $content . ' Successfully Deleted';
    $ci->messages['archived'] = $content . ' Archived';
    $ci->messages['add_failed'] = 'Please Fill-up All the Input Fields Needed';
    $ci->messages['edit_failed'] = 'Please Fill-up All the Input Fields Needed';
    $ci->messages['delete_failed'] = 'This ' .$content. ' Cannot be Deleted';
    $ci->messages['action_failed'] = 'No Items Selected';
    $ci->messages['active'] = $content. ' Activated';
    $ci->messages['inactive'] = $content. ' Deactivated';
    $ci->messages['accept'] = $content. ' Accepted';
    $ci->messages['decline'] = $content. ' Declined';
    $ci->messages['server_error'] = 'Request Failed';
}

/**
 * Retrieve specific message to be shown
 * @param type $message_type
 */
function get_message($message_type)
{
    $ci = &get_instance();
    $message = array();
    
    $message['message_type'] = $message_type;
    
    if ( isset($message_type) ) // check if a message type is passed.
    {
        if ( isset($ci->messages[$message_type]) ) // countercheck for specified messages.
        {
            if($message_type === 'add_success' OR $message_type === 'edit_success' OR $message_type === 'delete_success' OR $message_type === 'archived')
            {
                $message['class'] = 'alert alert-success';
            } 
            if($message_type === 'add_failed' OR $message_type === 'edit_failed' OR $message_type === 'delete_failed' OR $message_type === 'action_failed')
            {
                $message['class'] = 'alert alert-danger';
            }
            $message['title'] = ucwords( str_replace('_', ' ', $message_type) );
            $message['message'] = $ci->messages[$message_type];
            
        } else // if not on the list use the message itself.
        {
            $message['class'] = 'alert alert-info';
            $message['title'] = 'Failed';
            $message['message'] = $message_type;
        }
        return $message;
    } else
    {
        return NULL;
    }
}

/**
 * Convert ascii and special characters to html equivalent
 * @access public
 * @param string $string String to be cleaned
 * @return string returns Html encoded string
 */
function clean_text($string)
{
    $ci = &get_instance();
    $ci->load->helper('string');
    
    $clean = htmlentities( ascii_to_entities($string), ENT_QUOTES, 'utf-8', FALSE);
    return $clean;
}

/*
* Method for validating content id.
* @access public
* @retrun bool Returns true if the validation was successful, otherwise false.
*/
function valid_id()
{
   $ci = &get_instance();
   $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|xss_clean');
   if ($ci->form_validation->run() == FALSE)
   {
       return false;
   } else
   {
       return true;
   }
}

/*
* Method for validating get id.
* @access public
* @retrun bool Returns true if the validation was successful, otherwise false.
*/
function valid_get_id($id)
{
   $ci = &get_instance();
   $validator = $ci->form_validation;
   
   $valid = TRUE;
   $valid = $valid && $validator->numeric($id);
   $valid = $valid && $validator->xss_clean($id);
   $valid = $valid && $validator->max_length($id, 10);
   
   return $valid;
}

/* End of file cms_helper.php */
/* Location: ./application/helpers/cms_helper.php */